package com.yangzheandroid.rxmvp.presenter.dialog;

import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yangzheandroid.retrofitutils.utils.LogUtil;
import com.yangzheandroid.rxmvp.APP;
import com.yangzheandroid.rxmvp.R;
import com.yangzheandroid.rxmvp.base.BaseView;
import com.yangzheandroid.rxmvp.base.RxPresenter;
import com.yangzheandroid.rxmvp.model.OnRequestListenter;
import com.yangzheandroid.rxmvp.model.dialog.DialogListIml;
import com.yangzheandroid.rxmvp.utils.DialogUtil;
import com.yangzheandroid.rxmvp.utils.ToastUtils;
import com.yangzheandroid.rxmvp.view.activity.dialog.DialogListActivity;
import com.yangzheandroid.rxmvp.view.activity.dialog.DialogListConstract;
import com.yangzheandroid.rxmvp.widget.dialog.BottomDialog;
import com.yangzheandroid.rxmvp.widget.dialog.PrettyProgressDialog;
import com.yangzheandroid.rxmvp.widget.dialog.SelectDialog;
import com.yangzheandroid.yzlibrary.OnPullListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Author：Jalen on 2016/9/2 21:58
 * Contact: studylifetime@sina.com
 */
public class DialogListPresenter extends RxPresenter<DialogListConstract.View> implements DialogListConstract.Presenter, OnPullListener {

    private static final String TAG = DialogListPresenter.class.getSimpleName();
    public DialogListIml mModel;
    public  Handler mHandler=null;
    private ArrayList<String> mResult;
    @Inject
    public DialogListPresenter() {
        mModel = new DialogListIml();
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mView.getPullToRefresh().stopLoadMore(true);
                mView.getPullToRefresh().stopRefresh(true);
            }
        };
        mResult = new ArrayList<>();
    }



    @Override
    public void start() {

        mView.showLoading();

        mModel.getData(new OnRequestListenter() {
            @Override
            public void onSuccess(List<String> list) {

                LogUtil.e(TAG, list.toString());

                mResult = (ArrayList<String>) list;

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        showData();
                        mView.hideLoading();
                    }
                });

            }

            @Override
            public void onError() {

            }
        });


    }

    private void showData() {
        mView.getRecycleView().setLayoutManager(new LinearLayoutManager(APP.getInstance()));
        mView.getRecycleView().setAdapter(new BaseQuickAdapter<String, BaseViewHolder>(R.layout.itemlist_dialoglist, mResult) {
            @Override
            protected void convert(final BaseViewHolder baseViewHolder, final String str) {
                baseViewHolder.setText(R.id.txt_content, str);
            }
        });

        //设置下拉刷新和上拉加载更多
        mView.getPullToRefresh().setOnPullListener(this);
        //设置点击事件
        mView.getRecycleView().addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                switch (position) {
                    case 0:
                        SelectDialog dialog1 = new SelectDialog(APP.getInstance());
                        dialog1.show();
                        break;
                    case 1:
                        BottomDialog bottomDialog = new BottomDialog(APP.getInstance());
                        bottomDialog.show();
                        break;
                    case 2:
                        PrettyProgressDialog progressDialog = new PrettyProgressDialog(APP.getInstance());
                        progressDialog.show();
                        break;
                    case 3:
                        DialogUtil.showCustomSimpleDialog(APP.getInstance(), "强制更新", new DialogUtil.SingleDialogCallBack() {
                            @Override
                            public void callBackPositive(DialogInterface dialog) {
                                ToastUtils.showToast("立即更新");
                            }
                        });
                        break;
                    case 4:
                        DialogUtil.showCustomDialog(APP.getInstance(), "强制更新", new DialogUtil.DoubleDialogCallBack() {
                            @Override
                            public void callBackPositive(DialogInterface dialog) {
                                ToastUtils.showToast("取消");
                                dialog.dismiss();
                            }

                            @Override
                            public void callBackNegative(DialogInterface dialog) {
                                ToastUtils.showToast("立即更新");
                                dialog.dismiss();
                            }
                        });
                        break;
                    case 5:
                        DialogUtil.showSimpleDialog(APP.getInstance(), "V7对话框", "强制更新", true, new DialogUtil.SingleDialogCallBack() {
                            @Override
                            public void callBackPositive(DialogInterface dialog) {
                                ToastUtils.showToast("立即更新");
                                dialog.dismiss();
                            }
                        });
                        break;
                    case 6:
                        DialogUtil.showDialog(APP.getInstance(), "V7对话框", "强制更新", true, new DialogUtil.DoubleDialogCallBack() {
                            @Override
                            public void callBackPositive(DialogInterface dialog) {
                                ToastUtils.showToast("取消");
                                dialog.dismiss();
                            }

                            @Override
                            public void callBackNegative(DialogInterface dialog) {
                                ToastUtils.showToast("立即更新");
                                dialog.dismiss();
                            }
                        });
                        break;
                    default:

                        break;

                }
            }
        });

    }


    @Override
    public void onRefresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                mHandler.sendEmptyMessage(0);
            }
        }).start();
    }

    @Override
    public void onLoadMore() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                mHandler.sendEmptyMessage(0);
            }
        }).start();
    }
}
